package com.export.xlsx.park;

import com.csvreader.CsvReader;
import com.export.xlsx.domain.Company;
import com.export.xlsx.domain.CompanyType;
import com.export.xlsx.service.BusinessService;
import com.export.xlsx.service.CompanyTypeService;
import com.export.xlsx.service.impl.BusinessServiceImpl;
import com.export.xlsx.service.impl.CompanyTypeServiceImpl;
import com.export.xlsx.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.nio.charset.Charset;

/**
 * 徐汇园
 *
 * 1. 徐汇园企业名单-给金电联行.csv
 *
 * Created by Ryu on 2018/1/29.
 */
@Slf4j
public class XhyData {

    BusinessService businessService = new BusinessServiceImpl();
    CompanyTypeService companyTypeService = new CompanyTypeServiceImpl();

    @Test
    public void testData() throws Exception {
        CsvReader r = new CsvReader("D://徐汇园utf8.csv", ',', Charset.forName("UTF-8"));
        System.out.println(r.readHeaders());
        // 逐条读取记录，直至读完

        String parkId = "1008";

        //记录条数
        int dataCount = 0;

        while (r.readRecord()) {

            //读取一行数据中的值
            String name = StringUtils.getStr(r.get(1));//企业名称
            String col2 = StringUtils.getStr(r.get(2));//注册类型
            String col3 = StringUtils.getStr(r.get(3));//内外资
            String col4 = StringUtils.getStr(r.get(4));//行业
            String col5 = StringUtils.getStr(r.get(5));//
            String taxpayerCode = StringUtils.getStr(r.get(6));//纳税人识别码
            String creditCode = StringUtils.getStr(r.get(7));//信用代码
            String establishmentDateStr = StringUtils.getStr(r.get(8));//开业日期
            String recordDate = StringUtils.getStr(r.get(9));//登记日期
            String regMonCount = StringUtils.getStr(r.get(10));//注册资本
            String regMonCurrency = StringUtils.getStr(r.get(11));//注册资本币种
            String companyAddr = StringUtils.getStr(r.get(12));//生产经营地址
            String legalPerson = StringUtils.getStr(r.get(13));//法人代表
            String telNum = StringUtils.getStr(r.get(14));//法人联系电话

            log.info("{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}", name, col2, col3, col4, col5, taxpayerCode, creditCode, establishmentDateStr, recordDate, regMonCount, regMonCurrency, companyAddr, legalPerson, telNum);

            String taxAddr = ""; //税务登记地址
            String regMon = regMonCount + regMonCurrency;
            String runPro = StringUtils.getStr(r.get(7));//一般性经营项目
            String comTypeStr = StringUtils.getStr(r.get(8));// 企业类型
            String regOrg=""; //登记机关



            //企业类型
            Integer comType = null;
            if(org.apache.commons.lang.StringUtils.isNotBlank(comTypeStr)){
                CompanyType companyType = companyTypeService.queryByName(comTypeStr);
                if(companyType != null){
                    comType = companyType.getCode();
                }
            }


            //成立日期
            String establishmentDateConvertedStr = "";
            if(org.apache.commons.lang.StringUtils.isNotBlank(establishmentDateStr)){
                establishmentDateConvertedStr = establishmentDateStr;
            }


            //第一步：查询现有表是否存在该企业
            Company company = new Company();
            company.setC1(name);
            company.setC2(parkId);

            if(null != businessService.findCompanyInfo(company)){
                //更新操作
                businessService.updateInfo(company, telNum, legalPerson, "", companyAddr, taxAddr, "", "", creditCode, taxpayerCode, regMon, runPro, comType, establishmentDateConvertedStr, recordDate, regOrg);
            } else {
                // 新增操作
                businessService.insertInfo(company, telNum, legalPerson, "", companyAddr, taxAddr, "", "", creditCode, taxpayerCode, regMon, runPro, comType, establishmentDateConvertedStr, recordDate, regOrg);
            }



            dataCount++;
        }

        log.debug("本次处理了徐汇园的 {} 条企业信息", dataCount);
        //关闭
        r.close();
    }

}
